var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    // tab切换
    currentTab: 0,
    dateStr: '2020-01',
    reward:0,
    list:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });

    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    //获取年份  
    var Y = date.getFullYear();
    //获取月份  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    var currentDate = Y + '-' + M

    that.receptorRequest(currentDate)
  },

  //获得奖励
  receptorRequest:function(date){
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        let openid = res.data
        wx.request({
          url: app.globalData.URL + '/app/team/myAward?flag=receptor&dateStr=' + date + '&openid='+openid,
          success: function (res) {
            console.log("/team/myAward,receptor",res)
            that.setData({
              reward1: res.data.jljb,
              dateStr1: res.data.dateStr,
              list1: res.data.list
            })
          }
        })
      },
    })
    that.setData({
      dateStr: date
    })
  },
  //发出奖励
  issuerRequest: function (date) {
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        let openid = res.data

        wx.request({
          url: app.globalData.URL + '/app/team/myAward?flag=issuer&dateStr=' + date + '&openid='+openid,
          success: function (res) {
            console.log("/team/myAward,issuer", res)
            that.setData({
              reward: res.data.jljb,
              dateStr: res.data.dateStr,
              list: res.data.list
            })
          }
        }) 
      },
    })
    that.setData({
      dateStr: date
    })
  },
  // 滑动切换tab
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });
  },

  // 点击tab切换
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
    if(that.data.currentTab == 0){
      that.receptorRequest(this.data.dateStr1)
    }else{
      that.issuerRequest(this.data.dateStr1)
    }
  },
  checkMonth:function(i) {
    if (i < 10) {
      i = "0" + i;
    }
    return i;
  },
  lastMonth1:function(){

    let currentDate = new Date(this.data.dateStr1);
    
    let lastDate = currentDate.setMonth(currentDate.getMonth() - 1);
    lastDate = new Date(lastDate)
    let lastYear = lastDate.getFullYear();
    let lastMonth = this.checkMonth(lastDate.getMonth() + 1); // 因日期中的月份表示为0-11，所以要显示正确的月份，需要 + 1

    lastDate = lastYear + '-' + lastMonth; 
    console.log("lastDate",lastDate)

    this.receptorRequest(lastDate)
  },

  afterMonth1:function(){
    let currentDate = new Date(this.data.dateStr1);
    let nextDate = currentDate.setMonth(currentDate.getMonth() + 1); 
    nextDate = new Date(nextDate);
    let nextYear = nextDate.getFullYear();
    let nextMonth = this.checkMonth(nextDate.getMonth() + 1); // 因日期中的月份表示为0-11，所以要显示正确的月份，需要 + 1

    nextDate = nextYear + '-' + nextMonth;
    console.log("nextDate", nextDate)
    this.receptorRequest(nextDate)
  },

  lastMonth2: function () {

    let currentDate = new Date(this.data.dateStr);

    let lastDate = currentDate.setMonth(currentDate.getMonth() - 1);
    lastDate = new Date(lastDate)
    let lastYear = lastDate.getFullYear();
    let lastMonth = this.checkMonth(lastDate.getMonth() + 1); // 因日期中的月份表示为0-11，所以要显示正确的月份，需要 + 1

    lastDate = lastYear + '-' + lastMonth;
    console.log("lastDate", lastDate)

    this.issuerRequest(lastDate)
  },

  afterMonth2: function () {
    let currentDate = new Date(this.data.dateStr);
    let nextDate = currentDate.setMonth(currentDate.getMonth() + 1);
    nextDate = new Date(nextDate);
    let nextYear = nextDate.getFullYear();
    let nextMonth = this.checkMonth(nextDate.getMonth() + 1); // 因日期中的月份表示为0-11，所以要显示正确的月份，需要 + 1

    nextDate = nextYear + '-' + nextMonth;
    console.log("nextDate", nextDate)
    this.issuerRequest(nextDate)
  },

  toDetail:function(e){
    console.log(e.currentTarget)
    var id = e.currentTarget.dataset.id
    var flag = e.currentTarget.dataset.flag
    var receptorId = e.currentTarget.dataset.receptorid
    var issuerId = e.currentTarget.dataset.issuerid
    var drawerId = e.currentTarget.dataset.drawerid
    var date = this.data.dateStr

    wx.navigateTo({
      url: '../rewardDetail/rewardDetail?id=' + id + '&flag=' + flag + '&receptorId=' + receptorId + '&issuerId=' + issuerId + '&drawerId=' + drawerId + '&dateStr=' + date,
    })
  }
  
})